//
//  YJNewFeatureViewController.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/11.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit

private let kReuseIdentifier = "ReuseIdentifier"

class YJNewFeatureViewController: UICollectionViewController {
    
    private let layout = UICollectionViewFlowLayout()
    
    // 父类没有init()这个构造函数,子类不需要使用overridr
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareColletionView()
    }
    
    private func prepareColletionView() {
        collectionView?.backgroundColor = UIColor.white
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.register(YJNewFeatureCell.self, forCellWithReuseIdentifier: kReuseIdentifier)
    }

}

// MARK: - collectionView数据源和代理方法
extension YJNewFeatureViewController {
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return YJNewFeatuerPageCount
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kReuseIdentifier, for: indexPath) as! YJNewFeatureCell
        
        cell.indexNum = indexPath.item
        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / UIScreen.main.bounds.width)
        
        // 判断当前是否是最后一页
        if page == YJNewFeatuerPageCount - 1 {
            let indexPath = IndexPath(item: page, section: 0)
            let cell = collectionView?.cellForItem(at: indexPath) as! YJNewFeatureCell
            
            cell.startButtonAnimation()
        }
    }
}

/// 自定义的cell
class YJNewFeatureCell: UICollectionViewCell {
    fileprivate var indexNum: Int = 0 {
        didSet {
            bgView.image = UIImage(named: "new_feature_\(indexNum + 1)")
            
            startButton.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initCustomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initCustomView() {
        contentView.addSubview(bgView)
        contentView.addSubview(startButton)
        
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        startButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-160)
        }
    }
    
    fileprivate func startButtonAnimation() {
        startButton.isHidden = false
        
        startButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.startButton.transform = CGAffineTransform.identity
        }, completion: nil)
    }
    
    @objc private func didClickStarBtn() {
        JPrint("点击开始按钮")
        // 切换到YJMainViewController
        AppDelegate.outSwitchRootViewController(YJMainViewController())
    }
    
    // MARK: - 懒加载
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("开始体验", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: .normal)
        
        btn.addTarget(self, action: #selector(didClickStarBtn), for: .touchUpInside)
        
        return btn
    }()
}
